package com.doyens.gmall.item.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.doyens.gmall.item.service.ItemService;
import com.doyens.gmall.list.client.ListFeignClient;
import com.doyens.gmall.model.product.BaseCategoryView;
import com.doyens.gmall.model.product.SkuInfo;
import com.doyens.gmall.model.product.SpuSaleAttr;
import com.doyens.gmall.product.client.ProductFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ThreadPoolExecutor;

@Service
public class ItemServiceImpl implements ItemService {

    @Autowired
    private ProductFeignClient productFeignClient;
    @Autowired
    private ThreadPoolExecutor threadPoolExecutor;
    @Autowired
    private ListFeignClient listFeignClient;

    @Override
    public Map<String, Object> getItem(Long skuId) {
        //不是自己查询数据库，是远程调用service-product完成查询所有数据
        Map<String, Object> map = new HashMap<>();
        CompletableFuture<SkuInfo> skuInfoCompletableFuture = CompletableFuture.supplyAsync(() -> {
            //获取skuinfo和sku的图片数据
            SkuInfo skuInfo = productFeignClient.getSkuInfo(skuId);
            map.put("skuInfo", skuInfo);
            return skuInfo;
        }, threadPoolExecutor);

        CompletableFuture<Void> categoryViewCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //获取三级分类的所有信息
            BaseCategoryView categoryView = productFeignClient.getCategoryView(skuInfo.getCategory3Id());
            map.put("categoryView", categoryView);
        }, threadPoolExecutor);

        CompletableFuture<Void> spuSaleAttrListCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //获取spuId下的所有销售属性和对应的值
            List<SpuSaleAttr> spuSaleAttrList = productFeignClient.getSpuSaleAttrListCheckBySku(skuId, skuInfo.getSpuId());
            map.put("spuSaleAttrList", spuSaleAttrList);
        }, threadPoolExecutor);

        CompletableFuture<Void> valuesSkuJsonCompletableFuture = skuInfoCompletableFuture.thenAcceptAsync(skuInfo -> {
            //获取sku销售属性的组合
            Map valueIdsMap = productFeignClient.getSkuValueIdsMap(skuInfo.getSpuId());
            String valuesSkuJson = JSONObject.toJSONString(valueIdsMap);
            map.put("valuesSkuJson", valuesSkuJson);
        }, threadPoolExecutor);

        CompletableFuture<Void> priceCompletableFuture = CompletableFuture.runAsync(() -> {
            //查询价格
            BigDecimal skuPrice = productFeignClient.getSkuPrice(skuId);
            map.put("price", skuPrice);
        }, threadPoolExecutor);

//        异步更新商品的热度
        CompletableFuture<Void> incrHotScoreCompletableFuture = CompletableFuture.runAsync(() -> {
            listFeignClient.incrHotScore(skuId);
        }, threadPoolExecutor);


        //map中是没有任何数据，子线程还没有执行完成
        //必须5个子线程都执行结束后，再执行return map操作。
        //anyOf 函数中可以放多个CompletableFuture对象，任何一个执行完毕，就执行结束
//        CompletableFuture.anyOf();
        //函数中可以放多个CompletableFuture对象，所有执行完毕，就执行结束

        //执行完所有的子线程后，往下执行
        CompletableFuture.allOf(skuInfoCompletableFuture,categoryViewCompletableFuture,
                spuSaleAttrListCompletableFuture,valuesSkuJsonCompletableFuture,priceCompletableFuture,incrHotScoreCompletableFuture).join();
        return map;
    }
}
